package com.git.bds.mr.hospital.service.sector;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.git.bds.mr.Constants;
import com.git.bds.mr.doctor.mapper.DoctorMapper;
import com.git.bds.mr.doctor.model.domain.Doctor;
import com.git.bds.mr.hospital.convert.DoctorConvert;
import com.git.bds.mr.hospital.convert.SectorConvert;
import com.git.bds.mr.hospital.mapper.sector.SectorMapper;
import com.git.bds.mr.hospital.model.domain.Sector;
import com.git.bds.mr.hospital.model.vo.DoctorVO;
import com.git.bds.mr.hospital.model.vo.SectorVO;
import com.git.bds.mr.page.PageParam;
import com.git.bds.mr.page.PageResult;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 科室表 服务实现类
 * </p>
 *
 * @author 成大事
 * @since 2022-09-19 16:43:59
 */
@Service
public class SectorServiceImpl extends MPJBaseServiceImpl<SectorMapper, Sector> implements SectorService {

    @Resource
    private DoctorMapper doctorMapper;

    /**
     * 获取所有科室
     *
     * @return {@link List}<{@link Sector}>
     */
    @Override
    public List<Sector> getAllSector() {
        return this.baseMapper.selectList(new LambdaQueryWrapper<Sector>()
                .select(Sector::getId,Sector::getName,Sector::getSynopsis)
                .eq(Sector::getHospitalId, StpUtil.getExtra(Constants.HOSPITAL_ID)));
    }

    /**
     * 获取所有科室 分页
     *
     * @param pageParam 页面参数
     * @return {@link PageResult}<{@link SectorVO}>
     */
    @Override
    public PageResult<SectorVO> getAllSectorPage(PageParam pageParam) {
        Page<Sector> sectorPage = this.baseMapper.selectPage(new Page<>(pageParam.getPageNo(), pageParam.getPageSize(),false)
                , new LambdaQueryWrapper<Sector>()
                        .select(Sector::getId, Sector::getName, Sector::getSynopsis)
                        .eq(Sector::getHospitalId, StpUtil.getExtra(Constants.HOSPITAL_ID)));
        List<SectorVO> sectorVOList = SectorConvert.INSTANCE.toSectorVO(sectorPage.getRecords());
        return new PageResult<>(sectorVOList,sectorPage.getSize());
    }

    /**
     * 获得此科室下所有医生
     *
     * @param sectorId 扇区id
     * @return {@link List}<{@link Doctor}>
     */
    @Override
    public List<Doctor> getAllDoctors(Long sectorId) {
        return doctorMapper.selectJoinList(Doctor.class,new MPJLambdaWrapper<Doctor>()
                .selectAll(Doctor.class)
                .leftJoin(Sector.class,Sector::getId,Doctor::getSectorId)
                .eq(Doctor::getSectorId,sectorId));
    }

    /**
     * 获取所有医生 分页
     *
     * @param sectorId  扇区id
     * @param pageParam 页面参数
     * @return {@link PageResult}<{@link DoctorVO}>
     */
    @Override
    public PageResult<DoctorVO> getAllDoctorsPage(PageParam pageParam, Long sectorId) {
        IPage<Doctor> doctorPage = doctorMapper.selectJoinPage(new Page<>(pageParam.getPageNo(), pageParam.getPageSize(),false), Doctor.class, new MPJLambdaWrapper<Doctor>()
                .selectAll(Doctor.class)
                .leftJoin(Sector.class, Sector::getId, Doctor::getSectorId)
                .eq(Doctor::getSectorId, sectorId));
        List<DoctorVO> doctorVOList = DoctorConvert.INSTANCE.toDoctorVO(doctorPage.getRecords());
        return new PageResult<>(doctorVOList, doctorPage.getSize());
    }
}
